<template>
	<view class="body">
		<view class="head" v-if="user">
			<view class="img" @click="head_image">
				<image class="logo" :src="user.active" mode="widthFix"></image>
			</view>
			<text>{{user.username}}</text>
		</view>
		<view v-else @click="login" class='login_view'>
			登录
		</view>
		<view class="nav" v-if="user">
		
			<view class="li" @click="push_qiandao">
				签到 ({{user.jifen_num}}积分)
			</view>
			<view class="li" @click="push_ziliao">
				个人资料
			</view>
			<view class="li" @click="push_shuoshuo">
				我的说说
			</view>
			<view class="li" @click="push_admin" v-if="roles.role_name=='管理员'">
				管理界面
			</view>
			<view class="li" @click="exit_out">
				退出
			</view>

		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: '',
				roles: ''
			}
		},
		onShow() {
			this.get_info()
		},
		mounted() {

		},
		onLoad() {
			// this.get_info()

		},
		methods: {
			push_shuoshuo(){
				uni.navigateTo({
					url: '../../page_shuoshuo/my_shuoshuo/my_shuoshuo'
				})
			},
			push_admin() {
				uni.navigateTo({
					url: '../../page_admin/admin/admin'
				})
			},
			push_ziliao() {
				uni.navigateTo({
					url: 'user_show/user_show'
				})
			},
			push_qiandao() {
				var that = this
				const db = uniCloud.database();
				uni.showLoading({
					title: '签到中~'
				})

				uniCloud.callFunction({
					name: 'get_jifenlist',
					data: {
						_wxopenid: that.user._wxopenid
					}
				}).then(res => {
					if (res.result.data.length == 0) {
						that.qindao()
					} else {
						uni.showToast({
							title: '今天已经签到了~'
						})
					}
				})
			},
			qindao() {
				var that = this
				let obj = {
					"jifen_num": that.user.jifen_num += 10,
					is_qiandao: true
				}
				const db = uniCloud.database();
				db.collection('user-list').doc(that.user._id).update(obj).then(res => {
					uniCloud.callFunction({
						name: 'qiandao_fun',
						data: {
							_wxopenid: that.user._wxopenid,
							jifen_num: that.user.jifen_num
						}
					}).then(resa => {
						uni.hideLoading()
						uni.showToast({
							title: '签到成功~'
						})
						that.get_info()
					})
				})
			},
			head_image() {
				var that = this
				uni.chooseImage({
					count: 1,
					success(res) {
						if (res.tempFilePaths.length > 0) {
							uni.showLoading({
								title: '上传中...'
							})
							let filePath = res.tempFilePaths[0]
							// callback方式 ，进行上传操作
							uniCloud.uploadFile({
								filePath: filePath, //要上传的文件对象
								cloudPath: Date.now() + '.jpg', //保存在云端的文件名，这里以时间戳命名
								success(res) {
									let imageUrl = res.fileID //云端返回的图片地址

									uniCloud.callFunction({
										name: 'getImage',
										data: {
											fileList: [imageUrl]
										}
									}).then(fil => {
										let imageUrl = fil.result.fileList[0].download_url
										const db = uniCloud.database();
										db.collection('user-list').doc(that
											.user._id).update({
											active: imageUrl
										}).then(user => {
											uni.showToast({
												title: '提交成功'
											})
											uni.hideLoading()
											that.get_info()
										})
									})
								},
								fail(err) {
									console.log(err)
								},
								complete() {
									uni.hideLoading()
								}
							});
						}
					}
				});

			},
			get_info() {

				uniCloud.callFunction({
					name: 'set_roles',
					data: {
						_id: this.user._id
					}
				}).then(res => {
					this.roles = res.result.data[0]
				})

				this.user = uni.getStorageSync('user')
				const db = uniCloud.database();
				db.collection('user-list').where({
					_wxopenid: this.user._wxopenid
				}).get().then(res => {
					this.user = res.result.data[0]
					this.$store.commit('UPDATEUSERS', this.user)
					uni.setStorageSync('user', this.user)
				})
			},
			exit_out() {
				uni.removeStorageSync('token')
				uni.removeStorageSync('user')
				this.user = ''
			},
			get_xygeng() {
				GetXygeng().then(res => {
					if (res.code == 200) {
						this.content = res.data
					}
				})
			},
			getimg() {
				let obj = {
					method: 'mobile',
					lx: 'c3',
					format: 'json'
				}
				suiji_img(obj).then(res => {
					this.imgurl = res.data.imgurl
				})
			},
			login() {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			}
		}
	}
</script>

<style scoped>
	.body {
		background: #f5f5f5;
	/* 	height: calc(100vh - 200rpx); */
	}

	.head {
		width: 100%;
		text-align: center;
		background: #376ea5;
		padding: 25rpx;
		box-sizing: border-box;
		border-radius: 5px;
	}

	.head .img {
		width: 200rpx;
		height: 200rpx;
		border-radius: 300rpx;
		overflow: hidden;
		margin: 0 auto;
		border: 5px solid #d5ca50;
		margin-bottom: 15px;

	}

	.head .img image {
		width: 100%;
		height: 100%;
	}

	.head text {
		margin-top: 30rpx;
		font-size: 50rpx;
		color: #fff;
	}

	.nav {
		width: 100%;

		border-bottom: 1px solid #f5f5f5;
		border-radius: 5px;
		margin-top: 70rpx;
	}

	.nav .li {
		width: 100%;
		height: 100rpx;
		box-sizing: border-box;
		padding: 0 25rpx;
		margin-top: 10rpx;
		background: #fff;
		float: left;
		text-align: center;
		line-height: 100rpx;
		font-size: 30rpx;
		color: #333;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.login_view {
		width: 300rpx;
		height: 300rpx;
		background: #376ea5;
		border-radius: 300rpx;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 auto;
		margin-top: 100px;
		font-size: 30px;
	}
</style>